package top.ffhome.system.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import cn.hutool.core.map.MapUtil;
import top.ffhome.common.audit.AuditService;
import top.ffhome.common.service.impl.BaseServiceImpl;
import top.ffhome.system.entity.SysConfig;
import top.ffhome.system.mapper.SysConfigMapper;
import top.ffhome.system.service.SysConfigService;

@Service
@AuditService(entity = SysConfig.class)
public class SysConfigServiceImpl extends BaseServiceImpl<SysConfig, SysConfigMapper> implements SysConfigService {
	@Override
	@CacheEvict(value = { "config_selectAll" }, allEntries = true)
	public void clearCache() {
	}

	@Override
	@Cacheable(value = "config_selectAll", unless = "#result == null || #result.size() == 0")
	public Map<String, String> selectAll() {
		Map<String, String> map = MapUtil.newHashMap();
		List<SysConfig> list = mapper.select(new SysConfig());
		for (SysConfig config : list) {
			map.put(config.getCrux(), config.getValue());
		}
		return map;
	}

	@Override
	public boolean checkCruxUnique(SysConfig config) {
		SysConfig dto = new SysConfig();
		dto.getParams().put("idNot", config.getId());
		dto.setCrux(config.getCrux());
		List<SysConfig> list = mapper.select(dto);
		if (list.size() > 0) {
			return false;
		}
		return true;
	}
}
